﻿using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using Vinno.Brand.Pack.Core.Options;

namespace Vinno.Brand.Pack.Extensions
{
    public static class ConfigurationExtensions
    {
        /// <summary>
        /// 从<see cref="IConfiguration"/>创建<see cref="ProvinceOptions"/>
        /// </summary>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public static ProvinceOptions GetProvincesOptions(this IConfiguration configuration)
        {
            var options = configuration.GetOptions<ProvinceOptions>("Provinces", (conf, Key) =>
            {
                IConfigurationSection section = configuration.GetSection(Key);
                if (!section.Exists())
                {
                    throw new MicroCloudException("Province配置选项不存在，请在appsetting.json配置 Province 节点");
                }
                var provinceOptions = new ProvinceOptions();
                provinceOptions.Provinces = section.Get<Dictionary<string, string>>();

                return provinceOptions;
            });

            return options;
        }
    }
}
